package com.dongdongshop.service.impl;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.domain.rep.BrandReq;
import com.dongdongshop.mapper.BrandMapper;
import com.dongdongshop.model.Brand;
import com.dongdongshop.service.BrandService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌分页查询
     * @param brandReq
     * @return
     */
    @Override
    public PageResult queryPage(BrandReq brandReq) {
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandReq,brand);
        PageHelper.startPage(brandReq.getPageNum(), brandReq.getPageSize());
        Page<Brand> brandList=brandMapper.queryPage(brand);
        return new PageResult<>(brandList.getPageNum(),brandList.getPages(),brandList.getTotal(),brandList.getResult());
    }


    /**
     * 品牌删除操作
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        brandMapper.delete(id);
    }


    /**
     * 增加修改操作
     * @param brandReq
     * @return
     */
    @Override
    public void save(BrandReq brandReq) {
        Brand brand = new Brand();
        BeanUtils.copyProperties(brandReq,brand);
        if (brand.getId()==null){
            brandMapper.add(brand);
        }else {
            brandMapper.update(brand);
        }
    }


    /**
     * 品牌批量删除操作
     * @param ids
     * @return
     */
    @Override
    public void batchRemove(List<Integer> ids) {
        brandMapper.batchRemove(ids);
    }



}
